package com.yanqu.road.dao.impl.maidian;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.maidian.UserDayOnlineData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserDayOnlineDataDaoImpl extends TempDataObjectDao<UserDayOnlineData> {

    public static String getUserDayOnlineDataSql = "select * from `t_u_user_day_online`";
    public Map<Long, UserDayOnlineData> getUserDayOnlineData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserDayOnlineData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDayOnlineDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserDayOnlineData data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateUserDayOnlineDataSql = "update `t_u_user_day_online` set online=?,loginTimes=?,resetTime=? where userId=?";
    @Override
    protected boolean updateData(UserDayOnlineData userDayOnlineData) {
        userDayOnlineData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userDayOnlineData.getOnline()));
        para.put(i++, new DbParameter(Types.INTEGER, userDayOnlineData.getLoginTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userDayOnlineData.getResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userDayOnlineData.getUserId()));
        if (getDb().executeNoneQuery(updateUserDayOnlineDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserDayOnlineDataSql = "insert into `t_u_user_day_online`(userId,online,loginTimes,resetTime) values (?,?,?,?)";
    @Override
    protected boolean addData(UserDayOnlineData userDayOnlineData) {
        userDayOnlineData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userDayOnlineData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userDayOnlineData.getOnline()));
        para.put(i++, new DbParameter(Types.INTEGER, userDayOnlineData.getLoginTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userDayOnlineData.getResetTime()));
        if (getDb().executeNoneQuery(insertUserDayOnlineDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserDayOnlineData parseData(ResultSet rs) throws SQLException {
        UserDayOnlineData userDayOnlineData = new UserDayOnlineData();
        userDayOnlineData.setUserId(rs.getLong("userId"));
        userDayOnlineData.setOnline(rs.getLong("online"));
        userDayOnlineData.setLoginTimes(rs.getInt("loginTimes"));
        userDayOnlineData.setResetTime(rs.getLong("resetTime"));

        return userDayOnlineData;
    }
}
